<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{

    public $table = 'orders';

    public $fillable = [
        'number',
        'total',
        'product_id',
        'is_pay',
        'payment',
        'pay_sn',
        'admin_id',
        'pay_time'
    ];

    protected $appends = [
        'total'
    ];

    protected $casts = [
        'is_pay' => 'boolean'
    ];

    public function setTotalAttribute($value)
    {
        $this->attributes['total'] = intval($value * 100);
    }

    public function getTotalAttribute($key)
    {
        return $this->attributes['total'] / 100;
    }

    public function product()
    {
        return $this->belongsTo(Product::class, 'product_id', 'id');
    }

    public function admin()
    {
        return $this->belongsTo(AppUsers::class, 'admin_id', 'id');
    }
}
